package com.easepal7506a.project.network;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import com.easepal7506a.project.manager.DataManager;
import com.easepal7506a.project.observer.ShowLoadingObserver;
import com.example.reslib.Constants;
import com.example.reslib.utils.PreferenceWrapper;
import com.ogawa.base.callback.GetOnlineCallback;
import com.ogawa.base.callback.GetStateCallback;
import com.ogawa.base.callback.GetTokenCallback;
import com.ogawa.base.callback.GetUpdataCallback;
import com.ogawa.base.callback.NormalCallback;
import com.ogawa.base.network.BaseResponse;
import com.ogawa.base.network.Event.Event;
import com.ogawa.base.network.Event.UpdataEvent;
import com.ogawa.base.network.RetrofitManager;
import com.ogawa.base.network.bean.BeanOnline;
import com.ogawa.base.network.bean.BeanStateInfo;
import com.ogawa.base.network.bean.BeanToken;
import com.ogawa.base.network.bean.BeanUpdata;
import com.ogawa.base.utils.CommonUtil;

import rx.Subscriber;


public class CommondManager {
    private static CommondManager mInst = null;
    private static Object mLock = new Object();


    private CommondManager() {

    }

    public static CommondManager getInst() {
        if (mInst == null) {
            synchronized (mLock) {
                mInst = new CommondManager();
            }
        }
        return mInst;

    }

    /**
     * 发送指令
     */
    public synchronized void sendCommond(String commond, String value) {

        Log.e("sendCommond", "key==" + commond + "------" + "value===" + value);
        Event event = new Event();
        event.setSn(DataManager.getInst().getmSn());
        event.setProgram(commond);//操作指令
        event.setParams(value);//指令参数
        event.setTransactionId(System.currentTimeMillis() + commond);

        RetrofitManager retrofitManager = RetrofitManager.getInstance();
        retrofitManager.setUri(Constants.URI_SEND_COMMOND);
        retrofitManager.setMethod(Constants.HTTP_POST);
        retrofitManager.setJsonBody(event);
        retrofitManager.sendCommondEvent(event, new Subscriber<BaseResponse>() {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Log.e("fhb_sendCommondEvent", "sendCommondEvent --- 失败");

            }

            @Override
            public void onNext(BaseResponse program) {
                Log.e("fhb_sendCommondEvent", "sendCommondEvent --- 成功 ---= " + program);
                if (!program.getErrCode().equals("0")) {

                }
            }
        });


    }

    /**
     * 发送指令
     */
    public synchronized void sendShortCommond(String value, final NormalCallback callback) {
        Log.e("SendCommond7506", "key==0701" + "------" + "value===" + value);
        if (callback == null) {
            return;
        }
        ShowLoadingObserver.getInst().noticeShowLoading();
        Event event = new Event();
        event.setSn(DataManager.getInst().getmSn());
        event.setProgram("0701");//操作指令
        event.setParams(value);//指令参数
        event.setTransactionId(String.valueOf(System.currentTimeMillis()) + "0701");

        RetrofitManager retrofitManager = RetrofitManager.getInstance();
        retrofitManager.setUri(Constants.URI_SEND_COMMOND);
        retrofitManager.setMethod(Constants.HTTP_POST);
        retrofitManager.setJsonBody(event);
        retrofitManager.sendCommondEvent(event, new Subscriber<BaseResponse>() {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                ShowLoadingObserver.getInst().noticeCancleLoading();
                callback.onFailure("网络请求失败,请检查网络");
                Log.e("fhb_sendCommondEvent", "sendCommondEvent --- 失败1");
            }

            @Override
            public void onNext(BaseResponse program) {

                if (!program.getErrCode().equals("0")) {
                    Log.e("fhb_sendCommondEvent", "sendCommondEvent --- 失败");
                    callback.onFailure(program.getErrMsg());
                } else {
                    Log.e("fhb_sendCommondEvent", "sendCommondEvent --- 成功 ---= " + program);
                    callback.onSuccess(program);
                }
                ShowLoadingObserver.getInst().noticeCancleLoading();
            }
        });


    }

    /**
     * 获取token
     */
    public void getToken(String sn, final GetTokenCallback callback) {
        if (callback == null) {
            return;
        }
        RetrofitManager retrofitManager = RetrofitManager.getInstance();
        retrofitManager.setUri(Constants.URI_TOKEN + "?sn=" + DataManager.getInst().getmSn());
        retrofitManager.setMethod(Constants.HTTP_GET);
        retrofitManager.setJsonBody(null);
        retrofitManager.getTokenEvent(sn, new Subscriber<BaseResponse<BeanToken>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Log.e("onError", e.toString() + "");
                callback.onGetTokenFailure();

            }

            @Override
            public void onNext(BaseResponse<BeanToken> program) {
                Log.e("fhb", "GETTOKEN --- 成功 ---= " + program);
                if (program != null && program.getErrCode().equals("0")) {
                    callback.onGetTokenSuccess(program);
                } else {

                    callback.onGetTokenFailure();
                }


            }
        });


    }


    /**
     * 获取状态
     *
     * @param params 查询参数，逗号分隔 如“0A01,0A02”
     */
    public void getState(final String params, final GetStateCallback callback) {
        if (callback == null) {
            return;
        }
        RetrofitManager retrofitManager = RetrofitManager.getInstance();
        Log.e("Url==", Constants.URI_STATE + "?params=" + params + "&sn=" + DataManager.getInst().getmSn());
        String replace = params.replace(",", "%2C");
        Log.e("replace", replace + "");
//        retrofitManager.setUri(Constants.URI_STATE+"?sn="+ DataManager.getInst().getmSn()+"&params="+params);
        retrofitManager.setUri(Constants.URI_STATE + "?params=" + CommonUtil.stringToUTF(params) + "&sn=" + DataManager.getInst().getmSn());
        retrofitManager.setMethod(Constants.HTTP_GET);
        retrofitManager.setJsonBody(null);
        retrofitManager.getStateEvent(DataManager.getInst().getmSn(), params, new Subscriber<BaseResponse<BeanStateInfo>>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Log.e("onError", e.toString() + "");
                callback.onStateFailure();

            }

            @Override
            public void onNext(BaseResponse<BeanStateInfo> program) {
                Log.e("fhb", "GETSTATE --- 成功 ---= " + program);
                if (program != null && program.getErrCode().equals("0")) {
                    callback.onGetStateSuccess(program);
                } else {

                    callback.onStateFailure();
                }

            }
        });

    }


    /**
     * 获取是否在线
     *
     * @param params 设备序号或mac地址
     * @param isMac  是否查询mac 0否 1是
     */
    public void getOnline(final Context context, final String params, int isMac, final GetOnlineCallback callback) {
        if (callback == null) {
            return;
        }
        RetrofitManager retrofitManager = RetrofitManager.getInstance();
        retrofitManager.setUri(Constants.URI_ONLINE + "?param=" + params + "&isMac=" + isMac);
        retrofitManager.setMethod(Constants.HTTP_GET);
        retrofitManager.setJsonBody(null);
        retrofitManager.getOnlineEvent(params, isMac, new Subscriber<BaseResponse<BeanOnline>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Log.e("onError", e.toString() + "");
                callback.onGetOnlineFailure();

            }

            @Override
            public void onNext(BaseResponse<BeanOnline> program) {
                Log.e("fhb", "GETONLINE --- 成功 ---= " + program);
                if (program.getErrCode().equals("0") && !TextUtils.isEmpty(program.getData().getSn())) {
                    PreferenceWrapper mSp = new PreferenceWrapper();
                    mSp.setStringValueAndCommit(com.ogawa.base.Constant.Constants.SN, program.getData().getSn());
                    DataManager.getInst().setmSn(program.getData().getSn());
                    callback.onGetOnlineSuccess(program);
                } else {
                    callback.onGetOnlineFailure();
                }


            }
        });


    }


    /**
     * 获取升级
     */
    public void getUpdata(Context context, final GetUpdataCallback callback) {
        if (callback == null) {
            return;
        }
        UpdataEvent event = new UpdataEvent();
        event.setPhoneid(CommonUtil.getAndroidId(context));
        event.setAppversion(String.valueOf(CommonUtil.getVersionCode(context)));//code
        event.setSystype(2);//androidm
        event.setMac("");
        event.setSysversion(String.valueOf(Build.VERSION.SDK_INT));


        RetrofitManager retrofitManager = RetrofitManager.getInstance();
        retrofitManager.setUri(Constants.URI_UPDATA);
        retrofitManager.setMethod(Constants.HTTP_POST);
        retrofitManager.setJsonBody(event);
        retrofitManager.upDataEvent(event, new Subscriber<BaseResponse<BeanUpdata>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Log.e("onError", e.toString() + "");
                callback.onGetOnlineFailure();

            }

            @Override
            public void onNext(BaseResponse<BeanUpdata> program) {
                Log.e("fhb", "GETONLINE --- 成功 ---= " + program);
                if (program.getErrCode().equals("0") && program.getData() != null) {

                    callback.onGetOnlineSuccess(program);
                } else {
                    callback.onGetOnlineFailure();
                }


            }
        });


    }


}
